//
//  IndicatorAnimation.m
//  HelloWorld
//
//  Created by ShiMac on 15/4/27.
//  Copyright (c) 2015年 guoyan. All rights reserved.
//

#import "IndicatorAnimation.h"


@implementation IndicatorAnimation

{
    CAShapeLayer *subLayer;

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


-(id)initWithFrame:(CGRect)frame{
    
    if(self=[super initWithFrame:frame])
    {
        
        subLayer=[CAShapeLayer layer];
        subLayer.fillColor=[UIColor clearColor].CGColor;
        subLayer.strokeColor=[UIColor cyanColor].CGColor;
        subLayer.lineWidth=2;
        subLayer.strokeEnd=0;

        subLayer.bounds=CGRectMake(0, 0, frame.size.width, frame.size.height);
        subLayer.path=[UIBezierPath bezierPathWithOvalInRect:self.bounds].CGPath;
        [self.layer addSublayer:subLayer];
        
        
    }
    
    return self;
    
    
    
}

-(void)startAnimation
{
    
    CABasicAnimation *b1=[CABasicAnimation animationWithKeyPath:@"strokeStart"];
    b1.duration=1;
    b1.fillMode=kCAFillModeForwards;
    b1.fromValue=@(0);
    b1.toValue=@(1);
    b1.speed=0.5;
    
    CABasicAnimation *b2=[CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    b2.duration=1;
    b2.fromValue=@(0);
    b2.toValue=@(1);
    b2.fillMode = kCAFillModeForwards;

    b2.speed=0.8;
    b2.removedOnCompletion=NO;
    
    CAAnimationGroup *arrowDisappear = [CAAnimationGroup animation];
    arrowDisappear.removedOnCompletion = YES;
    arrowDisappear.fillMode = kCAFillModeForwards;
    arrowDisappear.duration = 2;
    arrowDisappear.animations = @[b1,b2];
    arrowDisappear.repeatCount=100;

    [subLayer addAnimation:arrowDisappear forKey:@"round"];
    
    
}
-(void)stopAnimation
{
 
    CAShapeLayer *layer=[subLayer presentationLayer];

    
    [subLayer removeAllAnimations];
    subLayer.strokeStart=layer.strokeStart;
    subLayer.strokeEnd=layer.strokeEnd;
    
    
}




@end
